package com.homedo.odin.solution.service;

import com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator;
import com.dangdang.ddframe.rdb.sharding.id.generator.self.IPIdGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.service.BaseService;
import com.homedo.odin.solution.contract.pc.SpecificationIdRequestBeanReq;
import com.homedo.odin.solution.dao.SpecificationMapper;
import com.homedo.odin.solution.dao.UserSpecificationMapper;
import com.homedo.odin.solution.enumcode.specification.UserSpecificationTypeEnum;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.entity.Specification;
import com.homedo.odin.solution.model.entity.UserSpecification;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p> 行业规范 服务实现类 </p>
 *
 * @author HarveyDing
 * @since 2018-03-08
 */
@Service
public class SpecificationServiceImpl extends BaseService<SpecificationMapper, Specification> {


    @Autowired
    UserBasicServiceImpl userBasicServiceImpl;

    @Autowired
    UserSpecificationMapper userSpecificationMapper;


    public Specification getSpecificationBySpecificationId(Long specificationId) {
        Specification specification = new Specification();
        specification.setSpecificationId(specificationId);
        baseMapper.selectOne(specification);
        return baseMapper.selectOne(specification);
    }

    public List<Specification> querySpecificationListForPri(String specificationName, Integer firstCassify,
            Integer secondClassify, Integer specificationStatus,
            Integer formType, Integer status) {
        return baseMapper
                .querySpecificationListForPri(specificationName, firstCassify, secondClassify, specificationStatus,
                        formType, status);

    }

    public Specification getSpecification(Long specificationId) {
        return baseMapper.getSpecification(specificationId);
    }

    public void updateSpecification(Specification specification) {
        baseMapper.updateById(specification);
    }

    public void auditSpecification(Long specificationId, Integer status, String auditOpinion) {
        baseMapper.auditSpecification(specificationId, status, auditOpinion);

    }

    public void updateDel(Long specificationId, Integer isDel) {
        baseMapper.updateDel(specificationId, isDel);
    }

    public List<Specification> querySpecificationListByClassify(Integer secondClassify) {
        return baseMapper.querySpecificationListByClassify(secondClassify);
    }

    public List<Specification> queryMySpecificationListForPc(Integer userId, Integer firstClassify,
            Integer secondClassify, String zone, Integer createTimeSort) {
        return baseMapper.queryMySpecificationListForPc(userId, firstClassify, secondClassify, zone, createTimeSort);
    }

    public List<Specification> querySpecificationListForPc(Integer userId, Integer firstClassify,
            Integer secondClassify,
            String zone, Integer createTimeSort, String keyword) {
        return baseMapper
                .querySpecificationListForPc(userId, firstClassify, secondClassify, zone, createTimeSort, keyword);
    }

    public Long addSpecification(Specification specification) {
        IdGenerator idGenerator = new IPIdGenerator();
        Long specificationId = (Long) idGenerator.generateId();
        specification.setSpecificationId(specificationId);
        baseMapper.insert(specification);
        return specificationId;
    }

    /**
     * 获取推荐的行业规范列表（不包含当前方案）
     */
    public List<Specification> queryRecommendSpecificationList(Long specificationId, Integer secondClassify) {
        return baseMapper.queryRecommendSpecificationList(specificationId, secondClassify);
    }

    /**
     * pc详情推荐发布前十方案（不包含当前方案）
     */
    public List<Specification> queryTheFirstTenSpecification(Long specificationId, Integer secondClassify,
            Integer number) {
        return baseMapper.queryTheFirstTenSpecification(specificationId, secondClassify, number);
    }

    public void addSpecificationPageView(Long specificationId) {
        baseMapper.addSpecificationPageView(specificationId);
    }

    public void addSpecificationDownload(Long specificationId) {
        baseMapper.addSpecificationDownload(specificationId);
    }

    /****************************liStsar*/
    /**
     * 新增行业规范下载记录
     * @param specificationIdRequest
     */
    public void addUserSolutionDownload(SpecificationIdRequestBeanReq specificationIdRequest) {
        UserSpecification userSpecification = userSpecificationMapper.getUserSpecification(specificationIdRequest.getUserId(),specificationIdRequest.getSpecificationId(),
            UserSpecificationTypeEnum.DOWNLOAD.getDbValue());
        //userSolution表中存在记录,则只新增下载量,否则还需插入下载记录
        if(userSpecification == null){
            //插入下载记录
            UserSpecification uSpecification = new UserSpecification();
            uSpecification.setCreateTime(new Date());
            uSpecification.setUserId(specificationIdRequest.getUserId());
            uSpecification.setActionType(UserSpecificationTypeEnum.DOWNLOAD.getDbValue());
            uSpecification.setSpecificationId(specificationIdRequest.getSpecificationId());
            uSpecification.setUserId(specificationIdRequest.getUserId());
            userSpecificationMapper.insert(uSpecification);
        }
        //新增下载量
        baseMapper.addSpecificationDownload(specificationIdRequest.getSpecificationId());
    }

    public PageInfo<Specification> querySpecificationListFormSelect(String keyword, Integer userId, PageBean pageBean) {
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        return new PageInfo<Specification>(baseMapper.querySpecificationListFormSelect(keyword, userId));
    }

    /****************************liyiEnd*/
}
